package com.icim.twyd.importer.job;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.icim.core.pojo.FtpConfigure;
import com.icim.core.service.IFtpTwydService;
import com.icim.twyd.importer.service.IRetXmlImportService;
import com.icim.util.FileUtil;
import com.icim.util.GzCompressUtil;

public class TwydImportJob {
    private static final String TRX_CSOT_SALESORDER = "WA_WORKORDER";
    private static final String TRX_CSOT_MTRL_IN = "CSOTOUTSOURCING";
    private static final Logger logger = LoggerFactory.getLogger(TwydImportJob.class);
    private IFtpTwydService ftpTwydService;

    private IRetXmlImportService retXmlImportService;

    public void setFtpTwydService(IFtpTwydService ftpTwydService) {
        this.ftpTwydService = ftpTwydService;
    }

    public IRetXmlImportService getRetXmlImportService() {
      return retXmlImportService;
    }

    public void setRetXmlImportService(IRetXmlImportService retXmlImportService) {
      this.retXmlImportService = retXmlImportService;
    }

    public void subMainProc() {

        FtpConfigure ftpConfigure = ftpTwydService.getFtpConfigure();
        String localTempPath = ftpConfigure.getLocalTemporaryPath();
        String localGzPath = ftpConfigure.getLocalGzPath();
        String localPath = localTempPath;

        logger.info("twyd importer job starting ......");
        
        String gzFileName = null;
        try {
            // 连接FTP
          ftpTwydService.connect();
          
          File[] xmlFiles = FileUtil.listFiles(localPath);
          for (File xmlFile : xmlFiles) {
            try {
              //File xmlFile = new File(localGzFileName);
              
              //importXMLProc(xmlFile);
              retXmlImportService.importXMLProc(xmlFile);
              
              // 将文件从原始路径 移动到OK路径
//              FileUtil.removeFile(xmlFile.getName(), localTempPath, ftpConfigure.getFtpOkPath());
              ftpTwydService.uploadFile(xmlFile,ftpConfigure.getFtpOkPath());

            } catch (Exception ex) {
                logger.error(ex.toString());
                    // 将文件从原始路径 移动到NG路径
//                FileUtil.removeFile(xmlFile.getName(), localTempPath, ftpConfigure.getFtpNgPath());
                ftpTwydService.uploadFile(xmlFile,ftpConfigure.getFtpNgPath());
                // TODO
                sendAlarm();

            } finally {

              //FileUtil.removeFile(fileName, localTempPath, localGzPath);
            }
          }
          FileUtil.deleteAllFiles(localPath, FileUtil.XML);
          ftpTwydService.disconnectFtp();
          

          // 连接FTP
          ftpTwydService.connect();
            // 获取文件列表
            List<String> fileNameList = ftpTwydService.listFiles(FileUtil.XML);

            // 遍历GZ文件
            for (String fileName : fileNameList) {

                gzFileName = fileName;
                logger.info("find file [{}]", fileName);

                // 下载GZ文件到本地
                ftpTwydService.downLoadFile(fileName);
                String localGzFileName = localPath + "/" + fileName;

                try {
                    File xmlFile = new File(localGzFileName);
                    
                    //importXMLProc(xmlFile);
                    retXmlImportService.importXMLProc(xmlFile);
                    
                    // 将文件从原始路径 移动到OK路径
                    ftpTwydService.removeFiles(xmlFile.getName(), true);

                } catch (Exception ex) {
                    logger.error(ex.toString());
                    if (!StringUtils.isEmpty(gzFileName)) {
                        // 将文件从原始路径 移动到NG路径
                      ftpTwydService.removeFiles(gzFileName, false);
                    }
                    // TODO
                    sendAlarm();

                } finally {

                  //FileUtil.removeFile(fileName, localTempPath, localGzPath);
                }

            }
            
        } catch (Exception e) {
            sendAlarm();
            logger.error(e.toString());
        } finally {

            // 断开连接
            try {
              FileUtil.deleteAllFiles(localPath, FileUtil.XML);
              ftpTwydService.disconnectFtp();
            } catch (IOException e) {
                logger.error(e.toString());
            }
        }
        
    }

   /* private void importXMLProc(File xmlFile) {
        // 解析XML，将xml转换为对象实体
      TwydMessageXmlEntity xmlEntity = (TwydMessageXmlEntity) XmlConvertUtil.file2Object1(xmlFile,
          TwydMessageXmlEntity.class);

      Ret_cus_box retCusBox = new Ret_cus_box();
      retCusBox.TwydXml2Entity(xmlEntity.getBOX());
      // 写入DB
      retCusBoxService.save(retCusBox);
      
      List<TwydCellXmlEntity> cellList = xmlEntity.getBOX().getCELL();
      for (TwydCellXmlEntity cell : cellList) {
        Ret_cus_prd retCusPrd = new Ret_cus_prd();
        retCusPrd.TwydXml2Entity(cell, retCusBox.getBoxid());
        // 写入DB
        retCusPrdService.save(retCusPrd);
      }
    }*/

    private void sendAlarm() {
        // TODO Auto-generated method stub

    }

}
